package com.cnblsp2.android.study.androidresources.originxml;

import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cnblsp2.android.study.androidresources.R;

public class OriginalXmlActivity extends Activity {

	private Button activity_originalxml_btn = null;

	private TextView activity_originalxml_showbooklist = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_originalxml);

		activity_originalxml_btn = (Button) this.findViewById(R.id.activity_originalxml_btn);
		activity_originalxml_showbooklist = (EditText) this.findViewById(R.id.activity_originalxml_showbooklist);

		activity_originalxml_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 根据Xml资源的ID获取解析该资源的解析器
				// XmlResourceParser是XmlPullParse的子类
				XmlResourceParser xmlResourceParser = getResources().getXml(R.xml.books);
				try {

					StringBuffer content = new StringBuffer("");

					// 如果还没有到XML的文档结尾处
					while (xmlResourceParser.getEventType() != XmlResourceParser.END_DOCUMENT) {

						// 如果遇到了开始标签
						if (xmlResourceParser.getEventType() == XmlResourceParser.START_TAG) {
							// 获取该标签的标签名
							String tagName = xmlResourceParser.getName();

							if (tagName.equals("book")) {

								String bookName = xmlResourceParser.getAttributeValue(null, "price");
								content.append("价格:");
								content.append(bookName);

								// 根据属性索引来获取属性值
								String bookPrice = xmlResourceParser.getAttributeValue(1);
								content.append("	出版日期: ");
								content.append(bookPrice);
								content.append("  书名: ");
								// 获取文本节点值
								content.append(xmlResourceParser.nextText());
							}
							content.append("\n");
						}
						xmlResourceParser.next();
					}

					activity_originalxml_showbooklist.setText(content.toString());

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}
	/**
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */

}
